﻿using Microsoft.Extensions.Configuration;


using System;

namespace dz.util.core
{
    /// <summary>
    /// 配置类
    /// </summary>
    public class Config
    {
        private IConfiguration? config;
        private static readonly Lazy<Config> instance = new Lazy<Config>(() => new Config());

        private Config() 
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;

            var builder = new ConfigurationBuilder()
            .SetBasePath(path)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            // 构建 IConfigurationRoot 实例
            config = builder.Build();
        }

        public static Config Instance
        {
            get
            {
                return instance.Value;
            }
        }
        /// <summary>
        /// 返回数据库连接串
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string GetConnectionString(string name)
        {
            return config.GetConnectionString(name);
        }

        /// <summary>
        /// 获得配置值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetValue(string key)
        {
            return config[key];
        }
    }

    
}
